package cn.easyutil.util.platform.amap;

import cn.easyutil.util.javaUtil.JsonUtil;
import cn.easyutil.util.javaUtil.HttpUtil;

import java.util.List;
import java.util.Map;

/**
 * 高德地图
 * @author spc
 *
 */
public class AmapUtil {

	/** 根据坐标获得实际地址位置url*/
	private  String url_getAdByPoint = "http://restapi.amap.com/v3/geocode/regeo?output=json&location=#{lng},#{lat}&key=#{key}";
	
	/** 根据位置获得实际地址的坐标url*/
	private String url_getPointByAd = "http://restapi.amap.com/v3/geocode/geo?key=#{key}&address=#{address}";
	
	/** 计算两地间的距离*/
	private  String url_calc = "http://restapi.amap.com/v3/distance?key=#{key}&origins=#{start}&destination=#{end}";
	/**
	 * 高德调用API时注册的key
	 */
	private  String amap_key = "8e4d4e9ff0c902c3beb36c76f64ac5e9";
	
	public AmapUtil(String amap_key,String url){
		this.amap_key = amap_key;
		this.url_getAdByPoint = url;
	}
	
	public AmapUtil(String amap_key){
		this.amap_key = amap_key;
	}
	
	public AmapUtil(){}
	/**
	 * 根据坐标获取实际地理位置
	 * @param lat
	 * @param log
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private  AddressBean getAddressBeanByPoint(String lat,String lng){//lng=120.111672&lat=30.304305&name=杭州市拱墅区水月街&adcode=330100
		String url = this.url_getAdByPoint.replace("#{lat}", lat).replace("#{lng}", lng).replace("#{key}", this.amap_key);
		String result = HttpUtil.doUrl(url,"GET");
		Map<String, Object> map = JsonUtil.jsonToMap(result);
		AddressBean addressBean = new AddressBean();
		if(map.get("info")!=null && map.get("info").equals("OK")){
			if(map.get("regeocode")!=null){
				Map<String,Object> regeocode = (Map<String, Object>) map.get("regeocode");
				if(regeocode.get("formatted_address")!=null){
					String fullAddress = regeocode.get("formatted_address").toString();
					//设置全称
					addressBean.setFullAddress(fullAddress);
				}
				if(regeocode.get("addressComponent")!=null){
					Map<String,Object> component = (Map<String, Object>) regeocode.get("addressComponent");
					addressBean.setAreaCode(component.get("citycode")==null?"":component.get("citycode").toString());
					addressBean.setCityName(component.get("city")==null?"":component.get("city").toString());
					addressBean.setCountryName(component.get("country")==null?"":component.get("country").toString());
					addressBean.setDistrictCode(component.get("adcode")==null?"":component.get("adcode").toString());
					addressBean.setDistrictName(component.get("district")==null?"":component.get("district").toString());
					addressBean.setLat(lat);
					addressBean.setLng(lng);
					addressBean.setProvinceName(component.get("province")==null?"":component.get("province").toString());
					addressBean.setTownCode(component.get("towncode")==null?"":component.get("towncode").toString());
					addressBean.setTownName(component.get("township")==null?"":component.get("township").toString());
				}
			}
		}
		return addressBean;
	}
	
	@SuppressWarnings("unchecked")
	public AddressBean getPointByAddress(String address){
		String url = url_getPointByAd.replace("#{key}", this.amap_key).replace("#{address}", address);
		String result = HttpUtil.doUrl(url,"GET");
		Map<String, Object> map = JsonUtil.jsonToMap(result);
		AddressBean addressBean = new AddressBean();
		if(map.get("status")!=null && map.get("status").equals("1")){
			if(map.get("geocodes")!=null){
				List<Map<String,Object>> geocodes = (List<Map<String,Object>>) map.get("geocodes");
				if(geocodes!=null && geocodes.size()>0){
					Map<String, Object> component = geocodes.get(0);
					addressBean.setAreaCode(component.get("citycode")==null?"":component.get("citycode").toString());
					addressBean.setCityName(component.get("city")==null?"":component.get("city").toString());
					addressBean.setCountryName(component.get("country")==null?"":component.get("country").toString());
					addressBean.setDistrictCode(component.get("adcode")==null?"":component.get("adcode").toString());
					addressBean.setDistrictName(component.get("district")==null?"":component.get("district").toString());
					addressBean.setProvinceName(component.get("province")==null?"":component.get("province").toString());
					addressBean.setTownCode(component.get("towncode")==null?"":component.get("towncode").toString());
					addressBean.setTownName(component.get("township")==null?"":component.get("township").toString());
					addressBean.setFullAddress(component.get("formatted_address")==null?"":component.get("formatted_address").toString());
					if(component.get("location")!=null){
						String[] split = component.get("location").toString().split(",");
						if(split.length >= 2){
							addressBean.setLng(split[0]);
							addressBean.setLat(split[1]);
						}
					}
				}
			}
		}
		return addressBean;
	}
	/**
	 * 根据经纬度获取坐标地区中文名称全称
	 * @param lat
	 * @param lng
	 * @return
	 */
	public String getAddressByPoint(String lat,String lng){
		return getAddressBeanByPoint(lat, lng).getFullAddress();
	}
	
	/**
	 * 计算两地间距离
	 * @param startLat
	 * @param startLng
	 * @param endLat
	 * @param endLng
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Long calcPoint(String startLat,String startLng,String endLat,String endLng){
		String url = url_calc.replace("#{key}", this.amap_key).replace("#{start}",startLng +","+ startLat).replace("#{end}", endLng +","+ endLat);
		String result = HttpUtil.doUrl(url,"GET");
		Long distance = 0l;
		Map<String, Object> map = JsonUtil.jsonToMap(result);
		if(map.get("status")!=null && map.get("status").equals("1")){
			if(map.get("results")!=null){
				List<Map<String,Object>> results = (List<Map<String, Object>>) map.get("results");
				if(results.size()>0){
					Map<String, Object> r = results.get(0);
					if(r.get("distance")!=null){
						distance = Long.valueOf(r.get("distance").toString());
						return distance;
					}
				}
			}
		}
		throw new RuntimeException(result);
	}
	
	/**
	 * 高德地址封装bean
	 * @author spc
	 *
	 */
	class AddressBean{
		private String fullAddress;//详细地址
		private String districtCode;//区县代号
		private String districtName;//区县名称
		private String provinceName;//省份名称
		private String cityName;//城市名称
		private String areaCode;//区号
		private String countryName;//国家名称
		private String townName;//街道，城镇名称
		private String townCode;//街道，城镇代号
		private String lat;
		private String lng;
		public String getFullAddress() {
			return fullAddress;
		}
		public void setFullAddress(String fullAddress) {
			this.fullAddress = fullAddress;
		}
		public String getDistrictCode() {
			return districtCode;
		}
		public void setDistrictCode(String districtCode) {
			this.districtCode = districtCode;
		}
		public String getDistrictName() {
			return districtName;
		}
		public void setDistrictName(String districtName) {
			this.districtName = districtName;
		}
		public String getProvinceName() {
			return provinceName;
		}
		public void setProvinceName(String provinceName) {
			this.provinceName = provinceName;
		}
		public String getCityName() {
			return cityName;
		}
		public void setCityName(String cityName) {
			this.cityName = cityName;
		}
		public String getAreaCode() {
			return areaCode;
		}
		public void setAreaCode(String areaCode) {
			this.areaCode = areaCode;
		}
		public String getCountryName() {
			return countryName;
		}
		public void setCountryName(String countryName) {
			this.countryName = countryName;
		}
		public String getTownName() {
			return townName;
		}
		public void setTownName(String townName) {
			this.townName = townName;
		}
		public String getTownCode() {
			return townCode;
		}
		public void setTownCode(String townCode) {
			this.townCode = townCode;
		}
		public String getLat() {
			return lat;
		}
		public void setLat(String lat) {
			this.lat = lat;
		}
		public String getLng() {
			return lng;
		}
		public void setLng(String lng) {
			this.lng = lng;
		}
	}
	
	public static void main(String[] args){
		System.out.println(JsonUtil.beanToJson(new AmapUtil().getPointByAddress("杭州")));
	}
}
